/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author error216
 */
public class HtmlEntitiesConverter {
    
    private static Map<String, Character> entities = new HashMap<String, Character>();
    
    static {
        entities.put("quot", '"');
        entities.put("amp", '&');
        entities.put("apos", '\'');
        entities.put("gt", '>');
        entities.put("lt", '<');
        entities.put("nbsp", ' ');
    }
    
    public static String convert(String string) {
        StringBuilder stringBuilder = new StringBuilder(string);
        int lastIndex = -1;
        int indexAmpersand;
        int indexSemicolon;
        
        while ((indexAmpersand = stringBuilder.indexOf("&", lastIndex + 1)) > -1
                && (indexSemicolon = stringBuilder.indexOf(";", indexAmpersand + 1)) > -1) {
            
            if (stringBuilder.charAt(indexAmpersand + 1) == '#') {
                if (stringBuilder.charAt(indexAmpersand + 2) == 'x') {
                    // hexadecimal notation &#xhhhh;
                    try {
                        char replacement = (char) Integer.parseInt(
                                stringBuilder.substring(indexAmpersand + 3, indexSemicolon)
                                .replace(" ", ""), 16);
                        stringBuilder.setCharAt(indexSemicolon, replacement);
                        stringBuilder.delete(indexAmpersand, indexSemicolon);
                    } catch (NumberFormatException ex) {
                        lastIndex++;
                    }
                } else {
                    // decimal notation &#dddd;
                    try {
                        char replacement = (char) Integer.parseInt(
                                stringBuilder.substring(indexAmpersand + 2, indexSemicolon)
                                .replace(" ", ""));
                        stringBuilder.setCharAt(indexSemicolon, replacement);
                        stringBuilder.delete(indexAmpersand, indexSemicolon);
                    } catch (NumberFormatException ex) {
                        lastIndex++;
                    }
                }
            } else {
                // name notation &name;
                Character replacement = entities.get(
                        stringBuilder.substring(indexAmpersand + 1, indexSemicolon)
                        .replace(" ", ""));
                if (replacement != null) {
                    stringBuilder.setCharAt(indexSemicolon, replacement);
                    stringBuilder.delete(indexAmpersand, indexSemicolon);
                } else {
                    lastIndex++;
                }
            }
        }
        
        return stringBuilder.toString();
    }
}
